package com.unis.utils


import com.alibaba.fastjson.JSON
import org.slf4j.{Logger, LoggerFactory}

import scala.collection.mutable.ListBuffer

object FastJsonUtil{
  private val logger: Logger = LoggerFactory.getLogger(getClass)

  /**
    * 通过httpClient发送post请求，从必可测接口获取数据，解析入库
    * @param rdd
    * @return
    */
  def DataParsing(rdd:String):ListBuffer[Object]={
    var arr = ListBuffer[Object]()

    try{
      if(rdd!=null){
        val json = JSON.parseArray(rdd)
        for(f<-0 to json.size()-1){
          var analyzeList=""
          var suggestList=""
          var overAllValuesList=""
          val datas = JSON.parseArray(rdd).get(f).toString
          val Details = JSON.parseObject(datas)
          val MachineId=Details.get("MachineId")
          val MachineName=Details.get("MachineName")
          val Communicate= Details.get("Communicate")
          val Operation=Details.get("Operation")
          val Health= Details.get("Health")
          val HealthDate= DateUtil.tranTimeToLong(Details.get("HealthDate").toString).toString.reverse
          val SuggestList=Details.get("SuggestList").toString
          val AnalyzeList=Details.get("AnalyzeList").toString
          if(SuggestList!="[]"){
            val suggestSize=JSON.parseArray(SuggestList).size()
            //SuggestList包含：等级（严重、较严重、中度、轻微、正常，未知）
            //维修方式(不能使用的、未检测、不推荐、等待、注意、择机、立刻)两个字段
            for(x<-0 to suggestSize-1){
              val Suggest=JSON.parseObject(JSON.parseArray(SuggestList).get(x).toString).values().toArray()
              val suggest=Suggest(0).toString+"_"+Suggest(1).toString
              suggestList+=suggest
            }
            suggestList
          }else{
            suggestList
          }
          if(AnalyzeList!="[]"){
            val analyzeSize=JSON.parseArray(AnalyzeList).size()
            for(x<- 0 to analyzeSize-1){
              //AnalyzeList包含：等级、故障模式、详细分析
              val Analyze=JSON.parseObject(JSON.parseArray(AnalyzeList).get(x).toString)
              val Description=Analyze.get("Description")
              val Recommendation=Analyze.get("Recommendation")
              val Peaks=Analyze.getJSONArray("Peaks")
              for(x<-0 to Peaks.size()-1){
                val peaks=Peaks.get(x).toString.replaceAll(",","_")
                val analyze=Description.toString+"_"+Recommendation.toString+"_"+peaks
                analyzeList+=analyze
              }
            }
            analyzeList
          }else{
            analyzeList
          }
          val OverAllValuesList=Details.get("OverAllValuesList")
          //OverAllValuesList是测点的基准信息和测点的振动和温度
          val locationsSize=JSON.parseArray(OverAllValuesList.toString).size()
          for(x<-0 to locationsSize-1 ) {
            val Locations = JSON.parseObject(JSON.parseArray(OverAllValuesList.toString).get(x).toString).values().toArray()
            val locations=Locations(0).toString+"_"+Locations(1).toString+"_"+Locations(2).toString +"_"+ Locations(3).toString+"_"+Locations(4).toString
            overAllValuesList+=locations
          }
          arr=List(HealthDate,MachineId,MachineName,Communicate,Operation,Health,suggestList,analyzeList,overAllValuesList)+:arr
        }
        arr
      }
    }catch {
      case ex:Exception=>logger.error("RDD Exection",ex.getMessage)
    }
    arr

  }

}